<?php

namespace App\Containers\AppSection\Profile\Actions;

use App\Containers\AppSection\File\Tasks\DownloadFileTask;
use App\Containers\AppSection\Profile\Tasks\FindProfileByIdTask;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;

class GetAvatarAction extends Action
{
    public function run(Request $request): StreamedResponse
    {
        $profile = app(FindProfileByIdTask::class)->run($request->id);
        if (empty($profile->avatar)) {
            throw new NotFoundException(__('ship::resource.not-found'));
        }

        return app(DownloadFileTask::class)->run($profile->avatar->id);
    }
}
